package example.documentbased.editorModule.controller
{
	import example.documentbased.common.components.DecisionPopUp;
	import example.documentbased.editorModule.models.EditorModuleModel;
	import example.documentbased.mainModule.controller.ICommonController;
	import example.documentbased.mainModule.controller.IMainController;
	import example.documentbased.mainModule.models.mappers.DocumentMapper;
	import example.documentbased.mainModule.models.vo.DocumentVO;
	import example.documentbased.mainModule.services.IDocumentService;
	
	import flash.events.Event;
	
	import mx.collections.ArrayList;
	
	import org.etherframework.common.utils.OU;
	import org.etherframework.controller.IReactiveCommand;
	import org.etherframework.controller.ReactiveCommand;
	import org.etherframework.controller.common.PopUpModel;
	
	import raix.reactive.IObservable;
	import raix.reactive.Observable;
	import raix.reactive.Unit;
	
	public class DeleteDocumentCommand extends ReactiveCommand
	{
		private var _document:DocumentVO;
		private var _editorModel:EditorModuleModel;
		private var _commonController:ICommonController;
		private var _editorController:IEditorController;
		private var _documentService:IDocumentService;
		private var _documentMapper:DocumentMapper;
		private var _mainController:IMainController;
		
		public function DeleteDocumentCommand(editorModel:EditorModuleModel, 
											  commonController:ICommonController,
											  mainController:IMainController,
											  editorController:IEditorController, 
											  documentService:IDocumentService, 
											  documentMapper:DocumentMapper)
		{
			super();
			
			_editorModel = editorModel;
			_commonController = commonController;
			_mainController = mainController;
			_editorController = editorController;
			_documentService = documentService;
			_documentMapper = documentMapper;
		}
		
		public function initialize(document:DocumentVO):IReactiveCommand
		{
			_document = document;
			markAsInitialized();
			
			return this;
		}
		
		override public function observable():IObservable
		{
			var model:PopUpModel = new PopUpModel("Delete Document", "Document '" + 
				_document.name+"' will be deleted. Are you sure?");
			
			return _commonController.getShowPopUpCommand(DecisionPopUp, model).observable()
				.mapMany(function(event:Event):IObservable
				{
					if (event.type == Event.COMPLETE)
					{
						if (_document.id)
						{
							return processing(_documentService.deleteDocument(
								_documentMapper.toDomain(_document)))
							.concatMany(function():IObservable {
								return onDocumentDelete();
							});
						}
					}
					return onDocumentDelete();
				});
		}
		
		private function onDocumentDelete():IObservable
		{
			_editorModel.closeDocument(_document);
			
			var openedDocuments:ArrayList = _editorModel.openedDocuments;
			var docToSelect:DocumentVO = openedDocuments.length ? 
				DocumentVO(openedDocuments.getItemAt(openedDocuments.length - 1)) : null;
			
			return _editorController.getSetCurrentDocumentCommand(docToSelect).observable()
				.concat(_mainController.getRefreshAllCommand().observable());
		}
	}
}